package com.hugang.newfeature.lambda;

import org.junit.Test;

import java.util.Comparator;

/**
 * Lambda表达式使用举例
 *
 * @author hg
 * @date 2020/6/24 16:08
 */
public class LambdaApplication {

    @Test
    public void test1(){
        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                System.out.println("test1");
            }
        };
        r1.run();

        //lambda表达式
        Runnable r2 = () -> System.out.println("lambda test1");
        r2.run();
    }

    @Test
    public void test2(){
        Comparator<Integer> com1 = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1,o2);
            }
        };
        int compare = com1.compare(12, 23);
        System.out.println(compare);
        System.out.println("************");
        //lambda表达式写法
        Comparator<Integer> com2 =(o1, o2) -> Integer.compare(o1,o2);
        int compare1 = com2.compare(32, 23);
        System.out.println(compare1);
        System.out.println("************");
        //方法引用写法
        Comparator<Integer> com3 = Integer :: compare;
        int compare2 = com3.compare(32, 23);
        System.out.println(compare2);

    }
}
